package cn.walls1717.ggkt.vod.service.impl;

import cn.walls1717.ggkt.model.vod.Teacher;
import cn.walls1717.ggkt.vo.vod.TeacherQueryVo;
import cn.walls1717.ggkt.vod.mapper.TeacherMapper;
import cn.walls1717.ggkt.vod.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 讲师服务实现类
 *
 * @author walls1717
 * @since 2022/06/29
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    /**
     * 条件分页查询
     */
    @Override
    public Page<Teacher> listPages(Integer pageNum, Integer pageSize, TeacherQueryVo teacherQueryVo) {
        Page<Teacher> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Teacher> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Teacher::getSort).orderByDesc(Teacher::getJoinDate);
        // 如果条件为空，直接分页查询
        if (teacherQueryVo == null) {
            return this.page(pageInfo);
        }

        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();

        if (!StringUtils.isEmpty(name)) {
            lqw.like(Teacher::getName, name);
        }
        if (!StringUtils.isEmpty(level)) {
            lqw.eq(Teacher::getLevel, level);
        }
        // joinDate >= joinDateBegin
        if (!StringUtils.isEmpty(joinDateBegin)) {
            lqw.ge(Teacher::getJoinDate, joinDateBegin);
        }
        // joinDate <= joinDateEnd
        if (!StringUtils.isEmpty(joinDateEnd)) {
            lqw.le(Teacher::getJoinDate, joinDateEnd);
        }
        // 有条件的分页查询
        return this.page(pageInfo, lqw);
    }
}
